//
//  SINACommonUserHelper.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/25.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINACommonUserHelper.h"
static SINACommonUserHelper * _SINACommonUserHelper;
@interface  SINACommonUserHelper()
@property(strong,nonatomic) SINAUserModel *userModel;
@end
@implementation SINACommonUserHelper
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    
    static dispatch_once_t onceToken;
    // 一次函数
    dispatch_once(&onceToken, ^{
        if (_SINACommonUserHelper == nil) {
            _SINACommonUserHelper = [super allocWithZone:zone];
        }
    });
    
    return _SINACommonUserHelper;
}
+ (instancetype)share{
    return  [[self alloc] init];
}


-(void)saveUserModel:(SINAUserModel *)userModel{
    _userModel = userModel;
    NSString *json = [userModel modelToJSONString];
    NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
    [userInfo setValue:json forKey:@"user"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}

-(SINAUserModel *)getUserModel{
    if (_userModel==nil) {
        NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
        // 创建新数组接收存储用户信息的数组内容
        NSString *userJson = [userInfo objectForKey:@"user"];
        if (userJson!=nil&&![userJson isEqualToString:@""]) {
            _userModel=[SINAUserModel modelWithJSON:userJson];
        }
        
    }
    return _userModel;
}

-(void)clearUserModel{
    NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
    [userInfo setValue:@"" forKey:@"user"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}


@end
